/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.jdbc.query;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author wy
 * @version v 0.1 2014-1-3 下午12:20:45 wy Exp $
 */
public class Update implements Query {

    /** table name */
    private final String table;

    /** where condition */
    private final Where where;

    /** set sql */
    private final StringBuilder setSQL;

    /** set param */
    private final List<Object> paramList;

    /**
     * @param table
     */
    public Update(String table) {
        this(table, null);
    }

    /**
     * @param table
     * @param where
     */
    public Update(String table, Where where) {
        this.table = table;
        if (where == null) {
            this.where = new Where();
        } else {
            this.where = where;
        }
        setSQL = new StringBuilder();
        paramList = new ArrayList<Object>();
    }

    /**
     * set update
     * 
     * @param column
     * @param value
     * @return
     */
    public Update set(String column, Object value) {

        if (setSQL.length() > 0) {
            setSQL.append(",");
        }
        setSQL.append(column).append("=").append("?");

        paramList.add(value);

        return this;
    }

    /**
     * 
     * @param wa
     * @return
     * @see com.easy.framwork.jdbc.query.Where#and(com.easy.framwork.jdbc.query.Where)
     */
    public Update and(Where wa) {
        where.and(wa);
        return this;
    }

    /**
     * 
     * @param wa
     * @return
     * @see com.easy.framwork.jdbc.query.Where#or(com.easy.framwork.jdbc.query.Where)
     */
    public Update or(Where wa) {
        where.or(wa);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andLike(java.lang.String,
     *      java.lang.Object)
     */
    public Update andLike(String column, Object value) {
        where.andLike(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orLike(java.lang.String,
     *      java.lang.Object)
     */
    public Update orLike(String column, Object value) {
        where.orLike(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andNotLike(java.lang.String,
     *      java.lang.Object)
     */
    public Update andNotLike(String column, Object value) {
        where.andNotLike(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orNotLike(java.lang.String,
     *      java.lang.Object)
     */
    public Update orNotLike(String column, Object value) {
        where.orNotLike(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andIsNull(java.lang.String)
     */
    public Update andIsNull(String column) {
        where.andIsNull(column);
        return this;
    }

    /**
     * 
     * @param column
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orIsNull(java.lang.String)
     */
    public Update orIsNull(String column) {
        where.orIsNull(column);
        return this;
    }

    /**
     * 
     * @param column
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andIsNotNull(java.lang.String)
     */
    public Update andIsNotNull(String column) {
        where.andIsNotNull(column);
        return this;
    }

    /**
     * 
     * @param column
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orIsNotNull(java.lang.String)
     */
    public Update orIsNotNull(String column) {
        where.orIsNotNull(column);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andEq(java.lang.String,
     *      java.lang.Object)
     */
    public Update andEq(String column, Object value) {
        where.andEq(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orEq(java.lang.String,
     *      java.lang.Object)
     */
    public Update orEq(String column, Object value) {
        where.orEq(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andNotEq(java.lang.String,
     *      java.lang.Object)
     */
    public Update andNotEq(String column, Object value) {
        where.andNotEq(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orNotEq(java.lang.String,
     *      java.lang.Object)
     */
    public Update orNotEq(String column, Object value) {
        where.orNotEq(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andGt(java.lang.String,
     *      java.lang.Object)
     */
    public Update andGt(String column, Object value) {
        where.andGt(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orGt(java.lang.String,
     *      java.lang.Object)
     */
    public Update orGt(String column, Object value) {
        where.orGt(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andLt(java.lang.String,
     *      java.lang.Object)
     */
    public Update andLt(String column, Object value) {
        where.andLt(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orLt(java.lang.String,
     *      java.lang.Object)
     */
    public Update orLt(String column, Object value) {
        where.orLt(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andGte(java.lang.String,
     *      java.lang.Object)
     */
    public Update andGte(String column, Object value) {
        where.andGte(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orGte(java.lang.String,
     *      java.lang.Object)
     */
    public Update orGte(String column, Object value) {
        where.orGte(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andLte(java.lang.String,
     *      java.lang.Object)
     */
    public Update andLte(String column, Object value) {
        where.andLte(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orLte(java.lang.String,
     *      java.lang.Object)
     */
    public Update orLte(String column, Object value) {
        where.orLte(column, value);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @param other
     * @return
     * @see com.easy.framwork.jdbc.query.Where#andBettwen(java.lang.String,
     *      java.lang.Object, java.lang.Object)
     */
    public Update andBettwen(String column, Object value, Object other) {
        where.andBettwen(column, value, other);
        return this;
    }

    /**
     * 
     * @param column
     * @param value
     * @param other
     * @return
     * @see com.easy.framwork.jdbc.query.Where#orBettwen(java.lang.String,
     *      java.lang.Object, java.lang.Object)
     */
    public Update orBettwen(String column, Object value, Object other) {
        where.orBettwen(column, value, other);
        return this;
    }

    /**
     * 
     * @param sql
     * @param params
     * @return
     * @see com.easy.framwork.jdbc.query.Where#append(java.lang.String,
     *      java.lang.Object[])
     */
    public Update append(String sql, Object... params) {
        where.append(sql, params);
        return this;
    }

    /**
     * Getter method for property <tt>where</tt>.
     * 
     * @where
     */
    public String getSQL() {
        StringBuilder sql = new StringBuilder(table.length() + setSQL.length()
                                              + where.getSQL().length() + 20);
        sql.append("UPDATE ").append(table).append(" SET ");
        sql.append(setSQL);
        if (where.getSQL().trim().length() > 0) {
            sql.append(" WHERE ").append(where.getSQL());
        }
        return sql.toString();
    }

    /**
     * Getter method for property <tt>getParams</tt>.
     * 
     * @return paramList
     */
    public Object[] getParams() {
        Object[] params = new Object[where.getParams().length + paramList.size()];
        System.arraycopy(paramList.toArray(), 0, params, 0, paramList.size());
        System.arraycopy(where.getParams(), 0, params, paramList.size(), where.getParams().length);
        return params;
    }

}
